package com.qiguliuxing.dts.admin.web;

import com.alibaba.fastjson.JSONObject;
import com.qiguliuxing.dts.core.util.HttpUtil;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.Map;

import static com.aliyun.oss.model.RoutingRule.Protocol.Http;

public class Test {

    public static void main(String[] args) {
        JSONObject param = new JSONObject();
        // Map<String, Object> param = new HashMap<>(8);
        param.put("touser", "obcjw68MrOxnlL3ndAAGloSLxIHk");
        param.put("template_id", "xZOSySoGqIy4YJMnPUDHT_VrTAPJVcvCyYc_kbivAh0");
        JSONObject data = new JSONObject();
        JSONObject thing1 = new JSONObject();
        thing1.put("value", "XC123456");
        JSONObject thing5 = new JSONObject();
        thing5.put("value", "0.01");
        JSONObject thing2 = new JSONObject();
        thing2.put("value", "2024-12-12");
        JSONObject date4 = new JSONObject();
        date4.put("value", "河北省张家口市");
        JSONObject thing15 = new JSONObject();
        thing15.put("value", "1369862335");
        data.put("character_string1", thing1);
        data.put("amount4", thing5);
        data.put("time8", thing2);
        data.put("thing9", date4);
        data.put("phone_number10", thing15);
        param.put("data", data);
        String send = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send";
        String sendUrl = send + "?access_token="
                + "87_a7yzW0bMtLifUZou12t1twrFqGy54iGiGp8YK7brexi_eBIr-81lhIEs9qm5ziDmJnL_qSkFFmUfhACm118g3rZ_ezucX6PCZTco2sumIDBOdulwE7iZZvyGKj8VDCbABAQYX";
        JSONObject post = httpRequest(sendUrl, "POST", param.toJSONString());
        System.out.println(param);
        System.out.println(post.toJSONString());
    }

    //处理http请求 requestUrl为请求地址 requestMethod请求方式，值为"GET"或"POST"
    public static JSONObject httpRequest(String requestUrl, String requestMethod, String outputStr){
        StringBuffer buffer=null;
        JSONObject jsonObject = null;
        try{
            URL url=new URL(requestUrl);
            HttpURLConnection conn=(HttpURLConnection)url.openConnection();
            conn.setConnectTimeout(30000);
            conn.setReadTimeout(30000);
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setRequestMethod(requestMethod);
            conn.connect();
            //往服务器端写内容 也就是发起http请求需要带的参数
            if(null!=outputStr){
                OutputStream os=conn.getOutputStream();
                os.write(outputStr.getBytes("utf-8"));
                os.close();
            }
            //读取服务器端返回的内容
            InputStream is=conn.getInputStream();
            InputStreamReader isr=new InputStreamReader(is,"utf-8");
            BufferedReader br=new BufferedReader(isr);
            buffer = new StringBuffer();
            String line = null;
            String str = "";
            while ((str = br.readLine()) != null) {
                buffer.append(str);
            }
            // 释放资源
            br.close();
            isr.close();
            is.close();
            is = null;
            conn.disconnect();
            jsonObject = JSONObject.parseObject(buffer.toString());
        }catch(Exception e){
            e.printStackTrace();
        }
        return jsonObject;
    }
}
